{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 集合的运算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 在对集合做运算时，不会影响原来的集合，而是返回一个运算结果\n",
    "\n",
    "## `&` 交集运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s_1 = set((1,2,3,4))\n",
    "s_2 = {3,4,5,6}\n",
    "\n",
    "rst = s_1 & s_2\n",
    "\n",
    "print(f'rst = {rst}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `|` 并集运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s_1 = set((1,2,3,4))\n",
    "s_2 = {3,4,5,6}\n",
    "\n",
    "rst = s_1 | s_2\n",
    "\n",
    "print(f'rst = {rst}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `-` 差集运算\n",
    "\n",
    "- `s_1 - s_2` —— `s_1` 中有的而 `s_2` 中没有的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s_1 = set((1,2,3,4))\n",
    "s_2 = {3,4,5,6}\n",
    "\n",
    "rst_1 = s_1 - s_2\n",
    "print(f'rst_1 = {rst_1}')\n",
    "\n",
    "rst_2 = s_2 - s_1\n",
    "print(f'rst_2 = {rst_2}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `^` 异或集运算\n",
    "\n",
    "- 获取只在一个集合中出现的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s_1 = set((1,2,3,4))\n",
    "s_2 = {3,4,5,6}\n",
    "\n",
    "rst = s_1 ^ s_2\n",
    "print(f'rst = {rst}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `<=`\n",
    "\n",
    "- 检查一个集合是否是另一个集合的子集\n",
    "- 如果一个集合中的元素全部都在另一个集合中出现，那么这个集合就是另一个集合的子集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s_1 = set((1,2,3,4))\n",
    "s_2 = {3,4,5,6}\n",
    "s_3 = set((1,2,3))\n",
    "\n",
    "rst_1 = s_1 <= s_2\n",
    "rst_2 = s_3 <= s_1\n",
    "print(f'rst_1 = {rst_1}')\n",
    "print(f'rst_2 = {rst_2}')"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "767d51c1340bd893661ea55ea3124f6de3c7a262a8b4abca0554b478b1e2ff90"
  },
  "kernelspec": {
   "display_name": "Python 3.9.5 64-bit",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.5"
  },
  "orig_nbformat": 2
 },
 "nbformat": 4,
 "nbformat_minor": 2
}